// components/deadCard/deadCard.js
import dayjs from "dayjs";
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      value: true,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    showDate: false,
    currentDate: +dayjs().startOf("day"),
    dateStr: "",
    formatterDate: (e, v) => {
      switch (e) {
        case "year":
          v = v + "年";
          break;
        case "month":
          v = v + "月";
          break;
        case "day":
          v = v + "日";
          break;
      }
      return v;
    },
    minDate: +dayjs().startOf("day").add(1, "day"),
    maxDate: +dayjs().endOf("day").add(5, "month"),

    fileList: [],

    formData: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    openDate() {
      this.setData({
        showDate: true,
      });
    },
    onCloseDate() {
      this.setData({
        showDate: false,
      });
    },
    onClose() {
      this.triggerEvent("close");
    },
    getInputVal(e) {
      this.setData({
        "formData.remark": e.detail.value,
      });
    },
    async upImages(e) {
      let file = await app.uploadImage(e.detail.file.tempFilePath, "outage");
      let fileList = JSON.parse(JSON.stringify(this.data.fileList));
      fileList = [
        {
          url: file,
        },
      ];
      this.setData({
        fileList,
        "formData.img_url": file,
      });
    },
    deleteImg(e) {
      let formData = this.data.formData;
      delete formData.img_url;
      this.setData({
        fileList: [],
        formData,
      });
    },
    getDateVal(e) {
      this.setData({
        currentDate: e.detail,
      });
    },
    getDate() {
      this.setData({
        "formData.up_date": dayjs(this.data.currentDate).format("YYYY-MM-DD"),
        showDate: false,
      });
    },
    confirm() {
      let formData = this.data.formData;
      if (!formData.up_date || !formData.remark || !formData.img_url) {
        wx.showToast({
          title: '请填写全部参数!',
          icon: 'none',
          dispatch: 3000
        })
        return;
      }
      this.triggerEvent("confirm", formData);      
    },
  },
});
